import pandas as pd
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

# 加载数据
data = pd.read_csv('wine.data', header=None)

# 提取类别1和2的数据
data_class1 = data[data[0] == 1]
data_class2 = data[data[0] == 2]
combined_data = pd.concat([data_class1, data_class2])

# 准备特征和标签
X = combined_data.iloc[:, 1:].values
y = combined_data[0].values

# PCA降维
pca = PCA(n_components=2)
pca_result = pca.fit_transform(X)

# LDA降维
lda = LinearDiscriminantAnalysis(n_components=1)  # 修改为1，因为只有两个类别
lda_result = lda.fit_transform(X, y)

# 输出PCA的两维特征
print("PCA results (first two dimensions):")
print(pca_result[:, 0:2])

# 输出LDA的一维特征
print("LDA results (previous 1D features):")
print(lda_result[:, 0])